﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsAppTest.Dao
{
    internal class ChangeDao
    {

        //使用hyl版本进行登录
        //String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
        //    "Initial Catalog=hylpersonsystem;" +
        //    "User ID=hyl;" +
        //    "Password=123456;";

        //使用window登录
        String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
            "Initial Catalog=hylpersonsystem;" +
            "Integrated Security=true;";



        //connect 无参
        public SqlConnection connect()
        {

            using (SqlConnection sc = new SqlConnection(connectionString))
            {  ///
                sc.Open();
                try
                {
                    //sc.Open();

                    // 检查连接状态
                    if (sc.State == ConnectionState.Open)
                    {
                        Console.WriteLine("数据库连接成功！");
                        MessageBox.Show("数据库连接成功！");
                        sc.Close();

                        // 连接成功后可以执行其他操作
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("数据库连接失败：" + ex.Message);
                    MessageBox.Show("数据库连接失败！");
                    sc.Close();
                }
                return sc;
            }///
        }
        //


        //添加人事变动信息
        public void CreateChange(String type, DateTime time, int userid)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "insert into change values(@Type,@Time,@Userid)";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("注册时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Type", type);
                command.Parameters.AddWithValue("@Time", time);
                command.Parameters.AddWithValue("@Userid", userid);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }

        //根据人事变动id删除人事变动信息
        public void DeleteChangeById(int id)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "delete from change where changeid=@ChangeId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("删除时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@ChangeId", id);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }


        //根据变动id更新人事变动信息
        public void UpdateChangeById(int id, string type, DateTime time)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "update change set type=@Type,time=@Time where changeid=@ChangeId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("更新时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@ChangeId", id);
                command.Parameters.AddWithValue("@Type", type);
                command.Parameters.AddWithValue("@Time", time);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }

        //根据用户id获取用户全部人员变动信息
        public List<WinFormsAppTest.pojo.Change> GetChangeByUserId(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from change where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Change> changes = new List<WinFormsAppTest.pojo.Change>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int changeid = reader.GetInt32(0);
                        String type = reader.GetString(1);
                        DateTime time = reader.GetDateTime(2);
                        int userid = reader.GetInt32(3);
                        WinFormsAppTest.pojo.Change f = new WinFormsAppTest.pojo.Change(changeid, type, time,userid);
                        changes.Add(f);
                    }
                    sc.Close();
                    return changes;
                }
            }
        }
        //


        //根据变动id获取用户全部人员变动信息
        public List<WinFormsAppTest.pojo.Change> GetChangeById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from change where changeid = @Changeid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Changeid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Change> changes = new List<WinFormsAppTest.pojo.Change>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int changeid = reader.GetInt32(0);
                        String type = reader.GetString(1);
                        DateTime time = reader.GetDateTime(2);
                        int userid = reader.GetInt32(3);
                        WinFormsAppTest.pojo.Change f = new WinFormsAppTest.pojo.Change(changeid, type, time, userid);
                        changes.Add(f);
                    }
                    sc.Close();
                    return changes;
                }
            }
        }
        //


        //根据变动类型获取用户全部变动信息
        public List<WinFormsAppTest.pojo.Change> GetChangeByType(string _type)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from change where type = @Type";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Type", _type);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Change> changes = new List<WinFormsAppTest.pojo.Change>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int changeid = reader.GetInt32(0);
                        String type = reader.GetString(1);
                        DateTime time = reader.GetDateTime(2);
                        int userid = reader.GetInt32(3);
                        WinFormsAppTest.pojo.Change f = new WinFormsAppTest.pojo.Change(changeid, type, time, userid);
                        changes.Add(f);
                    }
                    sc.Close();
                    return changes;
                }
            }
        }
        //

        //通过获取用户全部人事变动信息
        public List<WinFormsAppTest.pojo.Change> GetChanges()
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from change";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                //command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<WinFormsAppTest.pojo.Change> changes = new List<WinFormsAppTest.pojo.Change>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int changeid = reader.GetInt32(0);
                        String type = reader.GetString(1);
                        DateTime time = reader.GetDateTime(2);
                        int userid = reader.GetInt32(3);
                        WinFormsAppTest.pojo.Change f = new WinFormsAppTest.pojo.Change(changeid, type, time, userid);
                        changes.Add(f);
                    }
                    sc.Close();
                    return changes;
                }
            }
        }
        //

    }
}
